package com.lb.jwitter.action;


import net.sf.json.JSONArray; 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import org.springframework.test.context.ContextConfiguration;
import com.lb.jwitter.util.ApplicationContextProvider; 
import net.sf.json.JSONObject; 
import java.util.Map; 
import com.opensymphony.xwork2.ActionContext; 
import net.sf.json.JSONSerializer; 
import java.text.DateFormat; 
import java.text.SimpleDateFormat; 
import java.util.ArrayList; 
import java.util.Collection; 
import java.util.Collections; 
import java.util.Date; 
import java.util.List; 
import java.util.GregorianCalendar; 
import javax.xml.datatype.DatatypeFactory; 
import javax.xml.datatype.XMLGregorianCalendar; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import com.lb.jwitter.service.ConditionReport; 
import com.lb.jwitter.util.Message; 
import com.opensymphony.xwork2.Action; 
import com.lb.jwitter.service.TblQuyenChiTietService;import com.lb.jwitter.service.TblTaiKhoan;import com.lb.jwitter.service.TblQLDTTXADonXinGiaHanHocPhi;
import com.lb.jwitter.service.TblQLDTTXADonXinGiaHanHocPhiService;
import com.lb.jwitter.service.TblQLDTTXAHocVien;
import com.lb.jwitter.service.TblQLDTTXAHocVienService;

import com.lb.jwitter.service.TblCanBo;
import com.lb.jwitter.service.TblCanBoService;

import com.lb.jwitter.service.TblQLDTTXAHocKyTrongNam;
import com.lb.jwitter.service.TblQLDTTXAHocKyTrongNamService;

import com.lb.jwitter.service.TblQLDTTXAHocKyTrongNam;
import com.lb.jwitter.service.TblQLDTTXAHocKyTrongNamService;

import com.lb.jwitter.service.TblQLDTTXAHocVienHocPhiMonHoc;

import com.lb.jwitter.service.TblQLDTTXAHocVienHocPhiMonHocService;

import com.lb.jwitter.service.TblQLDTTXAHocVienHocPhiService;

import com.lb.jwitter.service.TblQLDTTXAMonHocService;

 /** 
 * @author QUOCVIET  
 * 
 */ 


@ContextConfiguration(locations="classpath:beans.xml") 
public class TblQLDTTXADonXinGiaHanHocPhiUpdateAction{
    private int pageSize; 
    public int getPageSize() {
       return pageSize;
 }

    public void setPageSize(int pageSize) { this.pageSize = pageSize; }

    private int page; 
    public int getPage() { return page; }

    public void setPage(int page) { this.page = page; }

    private int pages; 
    public int getPages() { return pages; }

    public void setPages(int pages) { this.pages = pages; }

    private String dataString; 
    public String getDataString() { return dataString; }

    private Message msg; 
    public Message getMsg() { return msg; }

    private int id; 
    public int getId() {
       return id;
 }

    public void setId(int id) {
       this.id = id;
 }

    private int hocVienId; 
    public void setHocVienId(int hocVienId) {
       this.hocVienId = hocVienId;
 }

    public int getHocVienId() {
       return hocVienId;
 }

    private String ngayNop; 
    public void setNgayNop(String ngayNop) {
       this.ngayNop = ngayNop;
 }

    public String getNgayNop() {
       return ngayNop;
 }

    private int canBoId; 
    public void setCanBoId(int canBoId) {
       this.canBoId = canBoId;
 }

    public int getCanBoId() {
       return canBoId;
 }

    private int tuHocKyId; 
    public void setTuHocKyId(int tuHocKyId) {
       this.tuHocKyId = tuHocKyId;
 }

    public int getTuHocKyId() {
       return tuHocKyId;
 }

    private int sangHocKyId; 
    public void setSangHocKyId(int sangHocKyId) {
       this.sangHocKyId = sangHocKyId;
 }

    public int getSangHocKyId() {
       return sangHocKyId;
 }

    private double tongTienGiaHan; 
    public void setTongTienGiaHan(double tongTienGiaHan) {
       this.tongTienGiaHan = tongTienGiaHan;
 }

    public double getTongTienGiaHan() {
       return tongTienGiaHan;
 }

    private String lyDo; 
    public void setLyDo(String lyDo) {
       this.lyDo = lyDo;
 }

    public String getLyDo() {
       return lyDo;
 }

    private boolean trangThai;
    private boolean duyet;
    private TblQLDTTXADonXinGiaHanHocPhi tblQLDTTXADonXinGiaHanHocPhi; 
    public void setTblQLDTTXADonXinGiaHanHocPhi (TblQLDTTXADonXinGiaHanHocPhi tblQLDTTXADonXinGiaHanHocPhi) {
        this.tblQLDTTXADonXinGiaHanHocPhi = tblQLDTTXADonXinGiaHanHocPhi;
 }

    private Collection<TblQLDTTXADonXinGiaHanHocPhi> tblQLDTTXADonXinGiaHanHocPhis; 
    public void setTblQLDTTXADonXinGiaHanHocPhis(Collection<TblQLDTTXADonXinGiaHanHocPhi> tblQLDTTXADonXinGiaHanHocPhis) {
        this.tblQLDTTXADonXinGiaHanHocPhis = tblQLDTTXADonXinGiaHanHocPhis; 
      } 

    private TblQLDTTXADonXinGiaHanHocPhiService tblQLDTTXADonXinGiaHanHocPhiService; 

    private TblQLDTTXAHocVienService tblQLDTTXAHocVienService; 

    private TblCanBoService tblCanBoService; 


    private TblQLDTTXAHocKyTrongNamService tblQLDTTXAHocKyTrongNamService; 

    private String tblQLDTTXAHocVienHocPhiMonHocStr; 
    public void setTblQLDTTXAHocVienHocPhiMonHocStr (String tblQLDTTXAHocVienHocPhiMonHocStr) 
 {     this.tblQLDTTXAHocVienHocPhiMonHocStr = tblQLDTTXAHocVienHocPhiMonHocStr;
 }

    public String getTblQLDTTXAHocVienHocPhiMonHocStr() {
       return tblQLDTTXAHocVienHocPhiMonHocStr;
 }

    private TblQLDTTXAHocVienHocPhiMonHocService tblQLDTTXAHocVienHocPhiMonHocService; 

    private TblQLDTTXAHocVienHocPhiService tblQLDTTXAHocVienHocPhiService; 

    private TblQLDTTXAMonHocService tblQLDTTXAMonHocService; 

	private int preUpdate(Message msg, TblQLDTTXADonXinGiaHanHocPhi obj ){ 
        msg.setPreMessage_b(true); 
        msg.setPreMessage_s("success"); 
        return 1; 
    } 
	private int postUpdate(Message msg, TblQLDTTXADonXinGiaHanHocPhi obj ){ 
        msg.setPostMessage_b(true); 
        msg.setPostMessage_s("success"); 
        return 1; 
    } 
  private TblTaiKhoan taiKhoanDangNhap; 
 public TblTaiKhoan getTaiKhoanDangNhap() { 
     return taiKhoanDangNhap; 
 } 
 public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) { 
     this.taiKhoanDangNhap = taiKhoanDangNhap; 
 } 
 private TblQuyenChiTietService tblQuyenChiTietService;
	 @Autowired // Injected by Spring 
     ApplicationContextProvider contextProvider; 
 public String execute() throws Exception 
{ 
 try{ 	
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_QLDTTXA_DonXinGiaHanHocPhi", "sua")){ 
        return "noprivilege"; 
    }        tblQLDTTXADonXinGiaHanHocPhiService = (TblQLDTTXADonXinGiaHanHocPhiService) context.getBean("tbl_QLDTTXA_DonXinGiaHanHocPhiClient"); 
        tblQLDTTXAHocVienService = (TblQLDTTXAHocVienService) context.getBean("tbl_QLDTTXA_HocVienClient"); 

        tblCanBoService = (TblCanBoService) context.getBean("tbl_CanBoClient"); 


        tblQLDTTXAHocKyTrongNamService = (TblQLDTTXAHocKyTrongNamService) context.getBean("tbl_QLDTTXA_HocKyTrongNamClient"); 

        tblQLDTTXAHocVienHocPhiMonHocService = (TblQLDTTXAHocVienHocPhiMonHocService) context.getBean("tbl_QLDTTXA_HocVienHocPhi_MonHocClient"); 

        tblQLDTTXAHocVienHocPhiService = (TblQLDTTXAHocVienHocPhiService) context.getBean("tbl_QLDTTXA_HocVien_HocPhiClient"); 

        tblQLDTTXAMonHocService = (TblQLDTTXAMonHocService) context.getBean("tbl_QLDTTXA_MonHocClient"); 

        msg = new Message(); 
        tblQLDTTXADonXinGiaHanHocPhi = tblQLDTTXADonXinGiaHanHocPhiService.gettblQLDTTXADonXinGiaHanHocPhi(new Long(id)); 
        tblQLDTTXADonXinGiaHanHocPhi.setHocVienId(tblQLDTTXAHocVienService.gettblQLDTTXAHocVien(new Long(hocVienId))); 
       DateFormat df = new SimpleDateFormat("dd/MM/yyyy"); 
       Date dd = df.parse(ngayNop); 
       GregorianCalendar c = new GregorianCalendar(); 
       c.setTime(dd);
       XMLGregorianCalendar d = DatatypeFactory.newInstance().newXMLGregorianCalendar(c); 
        tblQLDTTXADonXinGiaHanHocPhi.setNgayNop(d); 
        tblQLDTTXADonXinGiaHanHocPhi.setCanBoId(tblCanBoService.gettblCanBo(new Long(canBoId))); 
        tblQLDTTXADonXinGiaHanHocPhi.setTuHocKyId(tblQLDTTXAHocKyTrongNamService.gettblQLDTTXAHocKyTrongNam(new Long(tuHocKyId))); 
        tblQLDTTXADonXinGiaHanHocPhi.setSangHocKyId(tblQLDTTXAHocKyTrongNamService.gettblQLDTTXAHocKyTrongNam(new Long(sangHocKyId))); 
        tblQLDTTXADonXinGiaHanHocPhi.setTongTienGiaHan(tongTienGiaHan); 
        tblQLDTTXADonXinGiaHanHocPhi.setLyDo(lyDo); 
        
           tblQLDTTXADonXinGiaHanHocPhi.setTrangThai(trangThai); 
           tblQLDTTXADonXinGiaHanHocPhi.setDuyet(duyet);

        preUpdate(msg,tblQLDTTXADonXinGiaHanHocPhi); 
       if (msg.isPreMessage_b()) 
       { 
           tblQLDTTXADonXinGiaHanHocPhiService.updatetblQLDTTXADonXinGiaHanHocPhi(tblQLDTTXADonXinGiaHanHocPhi); 
           postUpdate(msg, tblQLDTTXADonXinGiaHanHocPhi); 
           if (msg.isPostMessage_b() == false) 
           { 
               return Action.ERROR; 
           } 
       }else{ 
           return Action.ERROR; 
       } 

       
       /*
        * 
        * 
        * 
        */
//    List < ConditionReport > cons = new ArrayList<ConditionReport>(); 
//    ConditionReport con = new ConditionReport(); 
//    List<TblQLDTTXAHocVienHocPhiMonHoc> tblQLDTTXAHocVienHocPhiMonHocList = tblQLDTTXAHocVienHocPhiMonHocService.findtblQLDTTXAHocVienHocPhiMonHocByCondition(cons, 0, 0); 
//    if(tblQLDTTXAHocVienHocPhiMonHocList != null && tblQLDTTXAHocVienHocPhiMonHocList.size() >0){ 
//         List<String> ids = new ArrayList<String>();  
//         for(TblQLDTTXAHocVienHocPhiMonHoc obj0 : tblQLDTTXAHocVienHocPhiMonHocList ){ 
//             Collections.addAll(ids, obj0.getId().toString());  
//         } 
//         tblQLDTTXAHocVienHocPhiMonHocService.deleteListtblQLDTTXAHocVienHocPhiMonHoc(ids);  
//     }
//    JSONArray jsonTblQLDTTXAHocVienHocPhiMonHoc = (JSONArray)JSONSerializer.toJSON(tblQLDTTXAHocVienHocPhiMonHocStr); 
//    for (int i = 0; i < jsonTblQLDTTXAHocVienHocPhiMonHoc.size(); i++) 
//     { 
//         JSONObject jsObj = jsonTblQLDTTXAHocVienHocPhiMonHoc.getJSONObject(i); 
//         TblQLDTTXAHocVienHocPhiMonHoc obj0 = new TblQLDTTXAHocVienHocPhiMonHoc(); 
//        obj0.setHocVienHocPhiId(tblQLDTTXAHocVienHocPhiService.gettblQLDTTXAHocVienHocPhi(new Long(jsObj.getInt("hocVienHocPhiId")))); 
//        obj0.setMonHocId(tblQLDTTXAMonHocService.gettblQLDTTXAMonHoc(new Long(jsObj.getInt("monHocId")))); 
//       obj0.setHocPhiMonHoc(jsObj.getDouble("hocPhiMonHoc")); 
//       obj0.setMienMon(jsObj.getBoolean("mienMon")); 
//       obj0.setTienTraLai(jsObj.getDouble("tienTraLai")); 
//       obj0.setDaDongHP(jsObj.getBoolean("daDongHP")); 
//       obj0.setThanhTien(jsObj.getDouble("thanhTien")); 
//       tblQLDTTXAHocVienHocPhiMonHocService.createtblQLDTTXAHocVienHocPhiMonHoc(obj0); 
//   } 
        return Action.SUCCESS; 
      } 
      catch(Exception e){ 
         System.out.println("Loi Update tbl_QLDTTXA_DonXinGiaHanHocPhi Action"); 
         e.printStackTrace(); 
         return Action.ERROR; 
 } 
 }

public boolean isTrangThai() {
	return trangThai;
}

public void setTrangThai(boolean trangThai) {
	this.trangThai = trangThai;
}

public boolean isDuyet() {
	return duyet;
}

public void setDuyet(boolean duyet) {
	this.duyet = duyet;
}

 } 
 
